package com.offer;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @Author: huangzhigao
 * @Date: 2023/3/4 18:06
 */
public class offer40 {
    public int[] getLeastNumbers(int[] arr, int k) {
        PriorityQueue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });

        for (int num : arr) {
            if (queue.size() < k) {
                queue.add(num);
            } else {
                if (queue.peek() > num) {
                    queue.poll();
                    queue.add(num);
                }
            }
        }

        int[] res = new int[k];
        int index = 0;
        while (!queue.isEmpty()) {
            res[index++] = queue.poll();
        }
        return res;
    }

    public static void main(String[] args) {
        PriorityQueue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        queue.add(1);
        queue.add(2);
        System.out.println(queue.peek());
    }

}
